package id.co.rho.scoring.cid.server.command.handler.impl;

import org.springframework.context.ApplicationContext;

import id.co.rho.scoring.cid.client.command.impl.VersioningSetDefaultCommandAction;
import id.co.rho.scoring.cid.client.command.impl.VersioningSetDefaultCommandResult;
import id.co.rho.scoring.cid.domain.Model;
import id.co.rho.scoring.cid.server.command.handler.CommandHandler;
import id.co.rho.scoring.cid.server.service.ScoringModelService;
import id.co.rho.scoring.cid.server.util.AppContext;

public class VersioningSetDefaultHandler
		implements
		CommandHandler<VersioningSetDefaultCommandAction, VersioningSetDefaultCommandResult> {

	@Override
	public VersioningSetDefaultCommandResult execute(
			VersioningSetDefaultCommandAction command) {
		VersioningSetDefaultCommandResult result = new VersioningSetDefaultCommandResult();
		ApplicationContext ctx = AppContext.getApplicationContext();
		ScoringModelService svc = ctx.getBean(ScoringModelService.class);
		Model model = svc.getModelById(command.getId());
		String count = svc.setAsDefault(command.getId(), model.getModelName());
		if (Integer.parseInt(count) > 0) {
			result.setOutput("One record saved...");
		} else {
			result.setOutput("Fail to update record...");
		}
		return result;
	}

	@Override
	public Class<VersioningSetDefaultCommandAction> getActionType() {
		return VersioningSetDefaultCommandAction.class;
	}

}
